home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / bin_tree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  8.0 KB  |  318 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  bin_tree.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #ifndef LEDA_BIN_TREE_H
  17. #define LEDA_BIN_TREE_H
  18.  
  19. //------------------------------------------------------------------------------
  20. //
  21. // bin_tree  
  22. //
  23. //     base class for all leaf oriented binary trees in LEDA
  24. //
  25. // Ijon Tichy  (1993)
  26. //
  27. //------------------------------------------------------------------------------
  28.  
  29.  
  30. #include <LEDA/basic.h>
  31.  
  32.  
  33. class bin_tree;
  34. class bin_tree_node;
  35.  
  36. typedef bin_tree_node* bin_tree_item;
  37.  
  38. typedef void (*DRAW_BIN_NODE_FCT)(double,double,void*,int);
  39. typedef void (*DRAW_BIN_EDGE_FCT)(double,double,double,double);
  40.  
  41.  
  42. //------------------------------------------------------------------------------
  43. // class bin_tree_node 
  44. //------------------------------------------------------------------------------
  45.  
  46. class bin_tree_node
  47. {  
  48.    friend class bin_tree;
  49.    friend class avl_tree;
  50.    friend class bb_tree;
  51.    friend class rb_tree;
  52.    friend class rs_tree;
  53.  
  54.  
  55.    GenPtr   k;              // key
  56.    GenPtr   i;              // info
  57.  
  58.    bin_tree_node* child[2]; // node: left and right child
  59.                             // leaf: successor and predecessor
  60.  
  61.    bin_tree_node* parent;   // pointer to parent
  62.  
  63.    bin_tree_node* corr;     // leaf: pointer to corresponding inner node
  64.                             // node: nil
  65.  
  66.    int   bal;               // rebalancing data
  67.  
  68.  
  69.    public:
  70.  
  71.  
  72.    bin_tree_node(GenPtr key, GenPtr inf, int b)
  73.    { k = key;
  74.      i = inf; 
  75.      bal = b;
  76.     }         
  77.  
  78.    bin_tree_node() { }         
  79.  
  80.    bin_tree_node(bin_tree_node* p)
  81.    { k = p->k;
  82.      i = p->i ;
  83.      bal = p->bal;
  84.     }         
  85.  
  86.    bool is_node()   { return (corr == nil);  }
  87.    bool is_leaf()   { return (corr != nil); }
  88.  
  89.    void set_bal(int x) { bal = x; }
  90.    int  get_bal()      { return bal; }
  91.  
  92.  
  93.    LEDA_MEMORY(bin_tree_node)
  94.  
  95. };
  96.  
  97.  
  98.  
  99. //------------------------------------------------------------------------------
  100. // class bin_tree
  101. //------------------------------------------------------------------------------
  102.  
  103. class bin_tree
  104.   protected:
  105.  
  106.   enum { left=0, right=1 };
  107.  
  108.   bin_tree_node ROOT;      // "super root" to avoid special cases in rotations
  109.                            // ROOT.child[left] points to real root node
  110.                            // ROOT.child[right] points to leftmost leaf
  111.   int count;            
  112.  
  113.  
  114.   // functions depending on used rebalancing method
  115.   // will be defined in derived classes (rb_tree, avl_tree, ...)
  116.  
  117.   virtual int leaf_balance() { return 0; }  // default balance value for leaves
  118.   virtual int node_balance() { return 0; }  // inner nodes
  119.   virtual int root_balance() { return 0; }  // root node
  120.  
  121.   virtual void insert_rebal(bin_tree_node*)   {}
  122.   virtual void del_rebal(bin_tree_node*, bin_tree_node*) {}
  123.  
  124.  
  125.  
  126.  
  127.   // other protected member functions
  128.  
  129.   bin_tree_node*& min_ptr() const 
  130.                            { return (bin_tree_node*&)ROOT.child[right];}
  131.  
  132.   void rotation(bin_tree_node*, bin_tree_node*, int);
  133.   void double_rotation(bin_tree_node*, bin_tree_node*, bin_tree_node*, int);
  134.  
  135.   void del_tree(bin_tree_node*);
  136.  
  137.   bin_tree_node* int_search(GenPtr) const;
  138.   bin_tree_node* search(GenPtr) const;
  139.   bin_tree_node* copy_tree(bin_tree_node*,bin_tree_item&) const;
  140.  
  141.  
  142.  
  143.   // functions depending on actual key type
  144.   // will be defined in dictionary and sortseq templates
  145.  
  146.   virtual int cmp(GenPtr x, GenPtr y) const { return compare(x,y); }
  147.  
  148.   virtual void clear_key(GenPtr&) const { }
  149.   virtual void clear_inf(GenPtr&) const { }
  150.   virtual void clear_iinf(GenPtr&)const { }
  151.  
  152.   virtual void copy_key(GenPtr&)  const { }
  153.   virtual void copy_inf(GenPtr&)  const { }
  154.  
  155.   virtual void print_key(GenPtr) const { }
  156.   virtual void print_inf(GenPtr) const { }
  157.  
  158.   virtual int  int_type() const { return 0; }
  159.  
  160.   //bin_tree_node* item(void* p) const { return (bin_tree_node*)p; }
  161.  
  162.  
  163. public:
  164.  
  165.   bin_tree_node* item(void* p) const { return (bin_tree_node*)p; }
  166.  
  167.   bin_tree_node*& root() const 
  168.                            { return (bin_tree_node*&)ROOT.child[left]; }
  169.  
  170.  
  171.   bin_tree_node* cyclic_succ(bin_tree_node* p) const { return p->child[right]; }
  172.   bin_tree_node* cyclic_pred(bin_tree_node* p) const { return p->child[left]; }
  173.  
  174.   bin_tree_node* succ(bin_tree_node* p) const
  175.   { return (p->child[right] == min_ptr()) ? 0 : p->child[right]; }
  176.  
  177.   bin_tree_node* pred(bin_tree_node* p) const
  178.   { return (p == min_ptr())  ?  0 : p->child[left] ; }
  179.  
  180.   bin_tree_node* first_item()  const               { return min_ptr(); }
  181.   bin_tree_node* next_item(bin_tree_node* p) const { return succ(p); }
  182.  
  183.   bin_tree_node* min() const { return min_ptr(); }
  184.   bin_tree_node* max() const { return (count>0) ? min_ptr()->child[left] : 0; }
  185.  
  186.  
  187.   bin_tree& conc(bin_tree&);
  188.   void split_at_item(bin_tree_node*,bin_tree&,bin_tree&);
  189.   void reverse_items(bin_tree_node*,bin_tree_node*);
  190.  
  191.  
  192.   bin_tree_node* insert(GenPtr,GenPtr,GenPtr=0);
  193.   bin_tree_node* insert_at_item(bin_tree_node*,GenPtr,GenPtr,GenPtr=0);
  194.   bin_tree_node* lookup(GenPtr) const;
  195.   bin_tree_node* locate(GenPtr) const;
  196.   bin_tree_node* locate_succ(GenPtr) const; 
  197.   bin_tree_node* locate_pred(GenPtr) const; 
  198.  
  199.   GenPtr   key(bin_tree_node* p)  const { return  p->k; }
  200.   GenPtr   inf(bin_tree_node* p)  const { return  p->i; }
  201.   GenPtr&  info(bin_tree_node* p) const { return  p->i; }
  202.  
  203.   void del(GenPtr);
  204.   void del_item(bin_tree_node* p);
  205.   void change_inf(bin_tree_node*,GenPtr);
  206.  
  207.   void clear();
  208.  
  209.   int size()   const { return count; } 
  210.   int empty()  const { return root() ? false : true ; }
  211.  
  212.  
  213.  
  214.   // construction, assignment, destruction
  215.  
  216.   bin_tree() {  count = 0; root() = nil; min_ptr() = nil; }
  217.  
  218.   bin_tree(const bin_tree&);
  219.   bin_tree& operator=(const bin_tree&);
  220.  
  221.   virtual ~bin_tree() { clear(); }
  222.  
  223.  
  224.  
  225.   // additional operations used by range and segment trees
  226.  
  227.   virtual void propagate_modification(int, GenPtr, GenPtr) {}
  228.  
  229.   bin_tree_node* l_child(bin_tree_node* p) const
  230.   { return p->is_leaf() ? 0 : p->child[left]; }
  231.  
  232.   bin_tree_node* r_child(bin_tree_node* p) const
  233.   { return p->is_leaf() ? 0 : p->child[right]; }
  234.  
  235.   int is_inner(bin_tree_node* p)  const
  236.   { return p->corr == 0; }
  237.  
  238.   bin_tree_node* parent(bin_tree_node* p)  const
  239.   { return (p==root()) ? 0 : p->parent; }
  240.  
  241.  
  242.   // miscellaneous
  243.  
  244.   void draw(DRAW_BIN_NODE_FCT, DRAW_BIN_NODE_FCT, DRAW_BIN_EDGE_FCT, 
  245.             bin_tree_node*, double, double, double, double, double);
  246.  
  247.   void draw(DRAW_BIN_NODE_FCT, DRAW_BIN_NODE_FCT, DRAW_BIN_EDGE_FCT, 
  248.             double, double, double, double);
  249.  
  250.   void print() const;
  251.   void print_tree(bin_tree_node*,int) const;
  252.  
  253. };
  254.  
  255.  
  256.  
  257. inline void bin_tree::rotation(bin_tree_node* p,bin_tree_node* q, int dir)
  258. { bin_tree_node* r = q->child[1-dir];
  259.   bin_tree_node* x = p->parent;
  260.   p->child[dir] = r;
  261.   q->child[1-dir] = p;
  262.   p->parent = q;
  263.   r->parent = p;
  264.   if (p == x->child[left])
  265.      x->child[left] = q;
  266.   else
  267.      x->child[right] = q;
  268.   q->parent = x;
  269.  
  270.   propagate_modification(4,p,r);
  271.   propagate_modification(5,q,p);
  272.   if( x!=&ROOT )
  273.     propagate_modification(6,x,q);
  274.  }
  275.  
  276.  
  277. inline void bin_tree::double_rotation(bin_tree_node* p, bin_tree_node* q, 
  278.                                       bin_tree_node* r, int dir1)
  279. { int dir2 = 1-dir1;
  280.   bin_tree_node* s = r->child[dir1];
  281.   bin_tree_node* t = r->child[dir2];
  282.   bin_tree_node* x = p->parent;
  283.   p->child[dir1] = t;
  284.   q->child[dir2] = s;
  285.   r->child[dir1] = q;
  286.   r->child[dir2] = p;
  287.   p->parent = r;
  288.   q->parent = r;
  289.   s->parent = q;
  290.   t->parent = p;
  291.  
  292.   if (p == x->child[left])
  293.      x->child[left] = r;
  294.   else
  295.      x->child[right] = r;
  296.  
  297.   r->parent = x;
  298.  
  299.   propagate_modification(7,p,t);
  300.   propagate_modification(8,q,s);
  301.   propagate_modification(9,r,p);
  302.   propagate_modification(10,r,q);
  303.   if( x!=&ROOT )
  304.     propagate_modification(11,x,r);
  305. }
  306.  
  307.  
  308.  
  309. // dummy I/O and cmp functions
  310.  
  311. inline void Print(const bin_tree&,ostream&) { }
  312. inline void Read(bin_tree&, istream&) { }
  313. inline int  compare(const bin_tree&,const bin_tree&) { return 0; }
  314.  
  315. #endif
  316.